//
//  RecommendViewController.m
//  BigJoy
//
//  Created by new-1020 on 2017/6/12.
//  Copyright © 2017年 Rowling. All rights reserved.
//

#import "RecommendViewController.h"
#import <MJRefresh.h>
#import "CommonCollectionViewCell.h"
#import "HeaderCollectionReusableView.h"
#import "XMSearchViewController.h"
#import "MerchantDetailVC.h"
#import "ErWeiMaSaoMiaoViewController.h"
#import "MessageViewController.h"

@interface RecommendViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UICollectionView * collectionView;
@end

@implementation RecommendViewController

-(UICollectionView *)collectionView {
    if (_collectionView == nil) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(XMW(170), XMW(240));
        layout.minimumInteritemSpacing = 10;
        layout.sectionInset = UIEdgeInsetsMake(0, 8, 0, 8);
        layout.headerReferenceSize=CGSizeMake(SCREEN_WIDTH, 400);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([CommonCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:@"Cell"];
         [self.collectionView registerNib:[UINib nibWithNibName:@"HeaderCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UID"];
    }
    return _collectionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.collectionView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.cutomNavBarView.backgroundColor = XMColor(248, 248, 248);
    MJRefreshAutoNormalFooter * footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        NSLog(@"ddddd");
        [self loadData];
    }];
    footer.stateLabel.textColor = [UIColor redColor];
    [footer setTitle:@"上啦加载更多  ↑" forState:1];
    self.collectionView.mj_footer = footer;
    
    @weakify(self);
    [[self.serchView.searchbutton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        XMSearchViewController * vc = [[XMSearchViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }];
    [[self.serchView.leftButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        ErWeiMaSaoMiaoViewController * vc = [ErWeiMaSaoMiaoViewController new];
        [self presentViewController:vc animated:YES completion:nil];
    }];
    [[self.serchView.mesaaageButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        MessageViewController * vc = [MessageViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }];
}

-(void)loadData {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.collectionView.mj_footer endRefreshing];
    });
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    MerchantDetailVC * vc = [MerchantDetailVC new];
    [self.navigationController pushViewController:vc animated:YES];
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    HeaderCollectionReusableView * reusableView;
    if (kind==UICollectionElementKindSectionHeader) {
        reusableView=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UID" forIndexPath:indexPath];
    }
    return reusableView;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 10;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CommonCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    return cell;
}

@end
